﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	ViewQ
//	visualizzazione delle mappe
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace Map_Fvg
{
    public partial class ViewQ : Form
    {
        public static float scalaXY;
        public static int scalaIniziale;
        public static string fileName;
        public static string IDtavol;

        public static int option_selected;
        public static int right_button_option;
        public static int left_button_option;

        //       private string coordVertices;
        public static string[] Selem = new string[4];
        int oldPosX2, oldPosY2;

        public static int readViewQ = 0;
        public static int drawMapQ = 0;
        public static int splitNodo;

        public ViewQ()
        {
            InitializeComponent();

            oldPosX2 = this.ClientSize.Width - button2.Location.X;
            oldPosY2 = this.ClientSize.Height - button2.Location.Y;

            oldSize = this.ClientSize;
            scalaIniziale = 0;

            originalPanel1Location = panel1.Location;
            originalPanel1Size = panel1.Size;

            originalPanel2Location = panel2.Location;
            originalPanel2Size = panel2.Size;

            originalPanel3Location = panel3.Location;
            originalPanel3Size = panel3.Size;

            DragDropHandler.Initialize();
            DragDropHandler.RegisterControl(this, true, true);

            GC.Collect();
            GC.WaitForPendingFinalizers();

        }

        public int openView(string FileName)
        {
            fileName = FileName;        //  ???????????????
            int iret = readFileLine(FileName);
            if (iret != 0)
            {
                MessageBox.Show("impossibile leggere il file " + FileName);
                goto L90;
            }
            iret = readFileText(FileName);
            if (iret != 0)
            {
                MessageBox.Show("impossibile leggere il file " + FileName);
                goto L90;
            }

            iret = readFilePoint(FileName);
            if (iret != 0)
            {
                MessageBox.Show("impossibile leggere il file " + FileName);
                goto L90;
            }

            iret = readFileArea(FileName);
            if (iret != 0)
            {
                MessageBox.Show("impossibile leggere il file " + FileName);
                goto L90;
            }

            scalaXY = 1.0F;
            insNodiDlg.select_point = 0;

            L90: return iret;

        }

          //*********************************************
        //      panel1_Paint
        //***********************************************
        static public Graphics dc0;
        static public int flagCursor;
        static public int attesaFlag;

        private void ThreadProcSafe()
        {
            Application.Run(new Attesa());
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (readViewQ == 0) return;
            noFilesSelected = 1;
            if (UnioneDialog.callOpenUnione == 1)
            {
                int iret = openUnione();
                if (iret > 0) return;
                UnioneDialog.callOpenUnione = 2;
            }
            if (UnioneDialog.callOpenUnione == 2) openUnione1();

            if (insNodiDlg.select_point == 1) this.Cursor = Cursors.UpArrow;
            if (insNodiDlg.select_point == 2) this.Cursor = Cursors.Cross;

            if (flagCursor == 1) this.Cursor = Cursors.UpArrow;
            if (flagCursor == 2) this.Cursor = Cursors.Cross;

            drawMapQ = 1;
            char[] delimiterChars0 = { ',', '/' };
            string[] words;

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            attesaFlag = 1;
            Thread newThread = new Thread(new ThreadStart(ThreadProcSafe));
            newThread.Start();

            float vValueMax = 0F, hValueMax = 0F;

           if (scalaXY != 1F)
            {
                vScrollBar1.Visible = true;
                hScrollBar1.Visible = true;

                float value = 100F / scalaXY;
                this.vScrollBar1.LargeChange = (int)Math.Round(value, 0);
                this.vScrollBar1.Value = oldY;
                vValueMax = (float)(100 - this.vScrollBar1.LargeChange);

                this.hScrollBar1.LargeChange = (int)Math.Round(value, 0);
                this.hScrollBar1.Value = oldX;
                hValueMax = (float)(100 - this.hScrollBar1.LargeChange);

            }

            Point RGmin = new Point();
            Point RGmax = new Point();
            RGmin.X = 0; // this.panel1.Size.Width;
            RGmin.Y = 0; // this.panel1.Size.Height;
            RGmax.X = this.panel1.Size.Width;
            RGmax.Y = this.panel1.Size.Height;

            Rectangle rect = new Rectangle(RGmin.X, RGmin.Y, RGmax.X, RGmax.Y);
            Region RG1 = new Region(rect);

            int Xdiff = Xmax - Xmin;
            int Ydiff = Ymax - Ymin;
            scalaX = (float)(RGmax.X - RGmin.X) / (float)Xdiff;
            scalaY = (float)(RGmax.Y - RGmin.Y) / (float)Ydiff;

            float scalaZ;
            if (scalaX < scalaY) scalaZ = scalaX;
            else scalaZ = scalaY;

            scalaZ = scalaXY * scalaZ;
            scalaX = scalaZ;
            scalaY = -scalaZ;
            Graphics dc = this.panel1.CreateGraphics();

            dc.Clip = RG1;
            if (Select_flag == (int)flagSelez.testo) dc.Clip = new Region(RcDraw);
            if (Select_flag == (int)flagSelez.punto) dc.Clip = new Region(RcDraw);
            if (Select_flag == (int)flagSelez.linea)
            {
                if (panel1.Contains(ctx)) dc.Clip = new Region(RcDraw);
            }
            if (Select_flag == (int)flagSelez.area)
            {
                if (panel1.Contains(ctx)) dc.Clip = new Region(RcDraw);
            }

            dc.Clear(Color.WhiteSmoke);
            dc0 = dc;

            Pen blackPen = new Pen(Color.Black, 1);

            float valueV = (float)vScrollBar1.Value;
            float valueH = (float)hScrollBar1.Value;

            Size Scrolloffset = new Size(0, 0);
            float fwidth = scalaX * Xdiff - 0.90F * this.panel1.Size.Width;
            Scrolloffset.Width = -(int)(fwidth * valueH / hValueMax);
            float fheight = -scalaY * Ydiff - 0.94F * this.panel1.Size.Height;
            Scrolloffset.Height = -(int)(fheight * valueV / vValueMax);

            if (scalaXY == 1F)
            {
                Xoff = (RGmax.X + RGmin.X - (int)(Xdiff * scalaX)) / 2;
                Yoff = RGmax.Y;
                Yoff = RGmax.Y - this.panel1.Height / 2 + (int)(Ydiff * scalaX) / 2;
                Yoff += 11;
            }
            else
            {
                Xoff = Scrolloffset.Width + RGmin.X;
                Yoff = Scrolloffset.Height + RGmax.Y + (int)((scalaXY - 1.0F) * (float)this.panel1.Height);
            }
            Rectangle Border = new Rectangle(Xoff, Yoff - (int)(Ydiff * scalaX), (int)(Xdiff * scalaX), (int)(Ydiff * scalaX));

            dc.DrawRectangle(blackPen, Border);

            //
            //      line_shp
            //
            Scarpata scr = new Scarpata();
            dc.RotateTransform(alpha);

            for (int i = 0; i < nShapes; ++i)  //    nShapes; ++i)
            {

                if (vectShapes[i].modifica == 2) continue;
                if (vectShapes[i].modifica == 5) continue;

                int nVertices = vectShapes[i].nVertices;

                int ipnt = vectShapes[i].pntpL;
                int pntSimb = vectShapes[i].pntSimb;

                if (vectS[pntSimb].selected == 0) continue;
                if (vectS[pntSimb].visib == 0) continue;

                Color icolor = vectS[pntSimb].icolor;
                string ss = vectS[pntSimb].tratto;
                int type = vectS[pntSimb].penType;

                float width = 0;
                float interpasso = 0;
                float diam = 0;
                string s1;
                float[] dashValues = { 0, 0 };
                switch (type)
                {
                    case 1:
                        int ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        width = float.Parse(s1, nfi);
                        break;
                    case 11:            // scarpata (ciglio)
                    case 12:            // scarpata (piede)
                    case 13:            // scarpata rivestita (ciglio)
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        width = float.Parse(words[3], nfi);
                        dashValues[0] = float.Parse(words[0], nfi);
                        dashValues[1] = float.Parse(words[1], nfi);
                        interpasso = float.Parse(words[2], nfi);
                        break;
                    case 26:         //  limite di bosco
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        width = float.Parse(words[4], nfi);
                        dashValues[0] = float.Parse(words[0], nfi);
                        dashValues[1] = float.Parse(words[1], nfi);
                        diam = float.Parse(words[2], nfi);
                        interpasso = float.Parse(words[3], nfi);
                        break;
                    case 33:
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        dashValues[0] = float.Parse(words[0], nfi);
                        dashValues[1] = float.Parse(words[1], nfi);
                        width = float.Parse(words[2], nfi);
                        break;
                    case 38:            //  limite di comune
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        width = float.Parse(words[2], nfi);
                        diam = float.Parse(words[0], nfi);
                        interpasso = float.Parse(words[1], nfi);
                        break;
                    case 40:         //  curva di livello intermedia
                    case 41:         //  curva di livello direttrice
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        width = float.Parse(s1, nfi);
                        break;
                    case 5:
                    case 42:     //  sentieri CAI
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        width = float.Parse(words[0], nfi);
                        dashValues[0] = 0F;
                        break;
                    case 43:        //  sentieri
                    case 44:        //  bordo di rotabile
                    case 60:        //  tracce sentieri
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        width = float.Parse(words[2], nfi);
                        dashValues[0] = float.Parse(words[0], nfi);
                        dashValues[1] = float.Parse(words[1], nfi);
                        break;
                    default:
                        width = 0F;
                        break;
                }
                if (width == 0F) continue;

                Pen iPen = new Pen(icolor, width);
                Point[] polygon = new Point[nVertices];

                if (type != 12 && dashValues[0] > 0F) iPen.DashPattern = dashValues;

                for (int j = 0; j < nVertices; ++j)
                {
                    polygon[j].X = (int)((PL[ipnt].X - Xmin) * scalaX) + Xoff;
                    polygon[j].Y = (int)((PL[ipnt].Y - Ymin) * scalaY) + Yoff;
                    ++ipnt;
                }
                if (type == 11 || type == 12 || type == 13)            // scarpata (ciglio)
                {
                    scr.scarpata(iPen, polygon, width, dashValues[0], dashValues[1], interpasso, dc, i, type);
                }

                else if (type == 38)    //  limite di comune
                {
                    dashValues[0] = diam;
                    dashValues[1] = interpasso - diam;
                    iPen.DashPattern = dashValues;
                    dc.DrawCurve(iPen, polygon);
                }

                else dc.DrawCurve(iPen, polygon);

                if (type == 26 && diam > 0)        //  limite di bosco
                {
                    scr.limiteDiBosco(iPen, polygon, width, dashValues[0], dashValues[1], interpasso, diam, dc, i);
                }

                continue;
            }

            if (Select_flag > 0 && selected_linea >=0)
            {
                if (ElencoSentieri.inum == 0)
                {
                    drawLinea(selected_linea, 0);
                }
                else
                {
                    int jmax = ElencoSentieri.inum;
                    for (int j = 0; j < jmax; ++j)
                    {
                        drawLinea(ElencoSentieri.numSelez[j], 0);
                    }
                }
            }

            dc.ResetTransform();  
 
            //
            //      text_shp
            //
            float rapp = 1.0F;

            if (scalaXY != 1.0F)
            {
                float fscala1 = scalaIniziale / scalaXY;
                rapp = 5000.0F / fscala1;
            }

            dc.RotateTransform(alpha);

            for (int i = 0; i < nShapesT; ++i)  //    nShapesT; ++i)
            {

                 if (vectShapesT[i].selected == 0) continue;
                 if (vectShapesT[i].modifica == 2) continue;
                 if (vectShapesT[i].modifica == 5) continue;

                 int pntSimb = vectShapesT[i].pntSimb;
                 if (vectT[pntSimb].selected == 0) continue;
                 int visib = vectT[pntSimb].visib;
                 if (visib == 0) continue;

                 Color icolor = vectT[pntSimb].icolor;
                 SolidBrush iBrush = new SolidBrush(icolor);
                 SolidBrush iBrush1 = new SolidBrush(Color.White);
                 Font iFont = vectT[pntSimb].iFont;
                 float curSize = iFont.Size;
                 float currentSize = curSize*rapp;
                 Font lFont = new Font(iFont.Name, currentSize, iFont.Style, iFont.Unit);

                 float angle = vectShapesT[i].GMrotation;
                 bool bAngle = false;
                 if (angle != 0.0F)
                 {
                      bAngle = true;
                 }

                 Point Pnt = new Point();
                 string stx = vectShapesT[i].testo.Trim();

                 char[] charArr = stx.ToCharArray();
                 int icar = 0;
                 bool flag = true;
                 foreach (char ch in charArr)
                 {
                      if (flag && ch == ' ')
                      {
                           ++icar;
                           flag = false;
                      }
                      if (ch != ' ') flag = true;
                 }

                 Pnt.X = (int)((vectShapesT[i].XX.X - Xmin) * scalaX) + Xoff;
                 Pnt.Y = (int)((vectShapesT[i].XX.Y - Ymin) * scalaY) + Yoff;
                 string space = " ";
                 float fspace = dc.MeasureString(space, lFont).Width;

                 int ipt = 0;
                 int ncar = 0;
                 int len = charArr.Length;
                 for (int ii = 0; ii < icar + 1; ++ii)
                 {
                      string sty = string.Empty;

                      if (icar == 0)
                      {
                           string stz = new string(charArr);
                           sty = stz;
                      }

                      if (icar > 0)
                      {
                           int j;
                           for (j = ipt; j < len; ++j) if (charArr[j] == ' ') break;
                           int len1 = j - ipt;
                           ncar = 1;
                           char[] charArr2 = new char[len1];
                           for (int k = 0; k < len1; ++k) charArr2[k] = charArr[k + ipt];
                           ipt = j + 1;
                           string stz = new string(charArr2);
                           sty = stz;
                           while (ipt < len)
                           {
                                if (charArr[ipt] != ' ') break;
                                ++ipt;
                                ++ncar;
                           }
                      }

                      SizeF stringSize = new SizeF();
                      stringSize = dc.MeasureString(sty, lFont);
                      float width1 = dc.MeasureString(sty, lFont).Width;
                      float height1 = dc.MeasureString(sty, lFont).Height;
                      float XX0 = 0.1F * width1;
                      float YY0 = 0.6F * height1;
                      float XX1 = 0.8F * width1;
                      float YY1 = 0.8F * height1;

                      if (bAngle)
                      {

                           dc.TranslateTransform(Pnt.X, Pnt.Y);
                           dc.RotateTransform(-angle);
                           RectangleF drawRect = new RectangleF(0, -YY0, width1, YY1);
                           dc.FillRectangle(iBrush1, drawRect);
                           dc.DrawString(sty, lFont, iBrush, drawRect);
                           dc.ResetTransform();
                           dc.RotateTransform(alpha);
                           if (icar > 0)
                           {
                                double angleR = Math.PI*angle/180.0;
                               Pnt.X += (int)((width1 + (float)ncar * fspace) * Math.Cos(angleR));
                               Pnt.Y -= (int)((width1 + (float)ncar * fspace) * Math.Sin(angleR));
                           }
                      }
                      else
                      {
                           RectangleF drawRect = new RectangleF(Pnt.X, Pnt.Y-YY0, width1, YY1);
                           dc.FillRectangle(iBrush1, drawRect);
                           dc.DrawString(sty, lFont, iBrush, drawRect);
                           if(icar > 0) Pnt.X += (int)(width1 + ncar*fspace);

                      }


                 }

            }
            dc.ResetTransform();

            //
            //      point_shp
            //
            rapp = 1.0F;
            if (scalaXY != 1.0F)
            {
                float fscala1 = scalaIniziale / scalaXY;
                rapp =  5000.0F / fscala1;
            }
      
            dc.RotateTransform(alpha);

            for (int i = 0; i < nShapesP; ++i)          //   nShapesP
            {

                if (vectShapesP[i].selected == 0) continue;
                if (vectShapesP[i].modifica == 2) continue;
                if (vectShapesP[i].modifica == 5) continue;

                int pntSimb = vectShapesP[i].pntSimb;
                if (vectP[pntSimb].selected == 0) continue;
                int visib = vectP[pntSimb].visib;
                if (visib == 0) continue;
                string codex = vectP[pntSimb].codice;
                int codice = vectP[pntSimb].simbolo;
                if (codice == 0) continue;

                Color icolor = vectP[pntSimb].icolor;
                SolidBrush iBrush = new SolidBrush(icolor);
                Font iFont = vectP[pntSimb].iFont;
                float angle = -vectShapesP[i].GMrotation;
                bool bAngle = false;
                if (angle != 0.0F)
                {
                    bAngle = true;
                    angle += 90.0F;
                }
                //          Pen iPen = new Pen(icolor, width);
                float dim = vectP[pntSimb].dimensione / 10.0F;
      //          float RGdiff1 = 1.13F * RGdiff * 25.4F / dc.DpiX;    //0.8571F *

                Point Pnt = new Point();
                Pnt.X = (int)((vectShapesP[i].XX.X - Xmin) * scalaX) + Xoff;
                Pnt.Y = (int)((vectShapesP[i].XX.Y - Ymin) * scalaY) + Yoff;

                if (codice != 1)
                {
                    int width = 0;
                    int height = 0;
                    string fileName = vectP[pntSimb].fileName;
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        fileName = fileName.Trim();

                        string fileX = FormIniz.bitmapFolder;
                        fileX = string.Concat(fileX, fileName);
                        string ss = vectP[pntSimb].paramSimb;

                        switch (codice)
                        {
                            case 5:         //  sorgente perenne
                            case 14:        //  croce isolata
                            case 22:        //  pino, altra conifera
                            case 38:        //  pino mugo
                            case 39:        //  faggio
                                int ipos = ss.IndexOf('/');
                                string s1 = ss.Substring(0, ipos);
                                words = s1.Split(delimiterChars0);
                                width = int.Parse(words[0]);
                                height = int.Parse(words[1]);
                                break;
                        }
                        Image newImage = Image.FromFile(fileX);
                        Bitmap Logo = new Bitmap(newImage);
                        Logo.MakeTransparent(Logo.GetPixel(1, 1));
                        newImage = (Image)Logo;

                        width = (int)(rapp * width);
                        height = (int)(rapp * height);

                        if (bAngle)
                        {
                            dc.TranslateTransform(Pnt.X, Pnt.Y);
                            dc.RotateTransform(angle);
                            Rectangle destRect = new Rectangle(0, 0, width, height);
                            dc.DrawImage(newImage, destRect);
                            dc.ResetTransform();
                            dc.RotateTransform(alpha);
                        }
                        else
                        {
                            Rectangle destRect = new Rectangle(Pnt.X, Pnt.Y, width, height);
                            dc.DrawImage(newImage, destRect);

                        }
                    }
                }
                if (codice == 1)
                {
                    Rectangle pntR = new Rectangle(Pnt.X, Pnt.Y, 4, 4);
                    dc.DrawEllipse(blackPen, pntR);
                }
            }
            dc.ResetTransform();


            //
            //      area_shp
            //

            dc.RotateTransform(alpha);

            for (int i = 0; i < nShapesA; ++i)      //
            {
                int nVertices = vectShapesA[i].nVertices;
                int ipnt = vectShapesA[i].pntPA;
                int nParts = vectShapesA[i].nParts;
                int[] ipart = new int[nParts + 1];
                if (nParts == 1)
                {
                    ipart[0] = 0;
                    ipart[1] = nVertices;
                }
                else
                {
                    string sp = vectShapesA[i].Parts_string;
                    words = sp.Split(delimiterChars0);
                    for (int j = 0; j < nParts; ++j) ipart[j] = int.Parse(words[j]);
                    ipart[nParts] = nVertices;
                }
                int pntSimb = vectShapesA[i].pntSimb;

                if (vectA[pntSimb].selected == 0) continue;
                if (vectShapesA[i].selected == 0) continue;
                if (vectShapesA[i].modifica == 2) continue;
                if (vectShapesA[i].modifica == 5) continue;


                int visib = vectA[pntSimb].visib;
                if (visib == 0) continue;
                string codice = vectA[pntSimb].codice;
                bool bEdif = false;
                if (codice.Contains("4A000ED")) bEdif = true;
                if (codice.Contains("4A000TE")) bEdif = true;
                if (codice.Contains("4A000IN")) bEdif = true;
                if (codice.Contains("4A000CU")) bEdif = true;
                if (codice.Contains("4A000AF")) bEdif = true;
                if (codice.Contains("4A000SM")) bEdif = true;
                if (codice.Contains("4A000BA")) bEdif = true;

                Color icolor = vectA[pntSimb].icolor;
                int type = vectA[pntSimb].codice_bordo;
                string ss = vectA[pntSimb].bordo;

                string s1;
                int ipos;
                float[] dashValues = { 0, 0 };
                float dist_linee = 0;
                float offset = 0;
                float width = 0F;
                switch (type)
                {
                    case 1:
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        width = float.Parse(s1);
                        break;
                    case 5:
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        width = float.Parse(words[2], nfi);
                        dashValues[0] = float.Parse(words[0], nfi);
                        dashValues[1] = float.Parse(words[1], nfi);
                        break;
                    default:
                        width = 0;
                        break;
                }

                if (width == 0F) continue;

                Pen iPen = new Pen(icolor, width);
                if (dashValues[0] > 0F) iPen.DashPattern = dashValues;


                Point[] polygon = new Point[1];

                for (int k = 0; k < nParts; ++k)
                {
                    ipnt = vectShapesA[i].pntPA + ipart[k];
                    int iVertices = ipart[k + 1] - ipart[k];
                    if (iVertices == 1) continue;
                    polygon = new Point[iVertices];
                    for (int j = 0; j < iVertices; ++j)
                    {
                        polygon[j].X = (int)((PA[ipnt].X - Xmin) * scalaX) + Xoff;
                        polygon[j].Y = (int)((PA[ipnt].Y - Ymin) * scalaY) + Yoff;
                        ++ipnt;
                    }
                    if (bEdif) dc.DrawLines(iPen, polygon);
                    else dc.DrawCurve(iPen, polygon);
                }

                type = vectA[pntSimb].codice_camp;
                if (type == 0) continue;
                ss = vectA[pntSimb].campitura;
                switch (type)
                {
                    case 1:
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        width = float.Parse(words[2], nfi);
                        dashValues[0] = float.Parse(words[0], nfi);
                        dashValues[1] = float.Parse(words[1], nfi);
                        dist_linee = float.Parse(words[3], nfi);
                        offset = float.Parse(words[4], nfi);
                        iPen.Width = width;
                        int lst = 0;
                        scr.arenile1(dc, iPen, nVertices, dashValues[0], dashValues[1], dist_linee, offset, i, lst);
                        break;

                    case 2:
                        ipos = ss.IndexOf('/');
                        s1 = ss.Substring(0, ipos);
                        words = s1.Split(delimiterChars0);
                        dashValues[0] = float.Parse(words[0], nfi);
                        dashValues[1] = float.Parse(words[1], nfi);
                        width = float.Parse(words[2], nfi);
                        offset = float.Parse(words[3], nfi);
                        dist_linee = float.Parse(words[4], nfi);
                        Pen mPen = new Pen(icolor, width);
                        int mst = 0;
                        scr.roccia(dc, mPen, nVertices, dashValues[0], dashValues[1], dist_linee, offset, i, mst);
                        break;

                    default:
                        width = 0;
                        break;
                }
            }

            if (Select_flag == (int)flagSelez.area)
            {
                drawArea(selected_area, 0);
            }
            //        dc.ResetTransform();

            float X0, Y0, X1, Y1;
            blackPen.Width = 0.2F;
            Font printFont1 = new Font("Arial", 9);
            int orizz = this.panel1.Width;
            int ivert = this.panel1.Height;
            int Xright = Xoff + (int)(Xdiff * scalaX);
            for (int i = 0; i < 20; ++i)
            {
                if (retic[i].X == 0) break;
                X0 = (retic[i].X - Xmin) * scalaX + Xoff;
                if (X0 < Xoff || X0 > Xright) continue;

                Y0 = (vert[0].Y - Ymin) * scalaY + Yoff;
                X1 = X0;
                Y1 = (vert[1].Y - Ymin) * scalaY + Yoff;

                dc.DrawLine(blackPen, X0, Y0, X1, Y1);
            }

            float Yofff = Yoff - (int)(Ydiff * scalaX);
            for (int i = 0; i < 20; ++i)
            {
                if (retic[i].Y == 0) break;
                Y0 = (retic[i].Y - Ymin) * scalaY + Yoff;
                if (Y0 < Yofff || Y0 > ivert - 17) continue;
                X0 = Xoff;  
                X1 = Xright;  
                Y1 = Y0;
                dc.DrawLine(blackPen, X0, Y0, X1, Y1);
            }



            if (Select_flag == (int)flagSelez.testo)
            {

                Point[] polygon = new Point[5];
                for (int j = 0; j < 4; ++j)
                {
                    polygon[j].X = (int)((PointSelect[j].X - Xmin) * scalaX) + Xoff;
                    polygon[j].Y = (int)((PointSelect[j].Y - Ymin) * scalaY) + Yoff;
                }
                polygon[4] = polygon[0];
                dc.DrawLines(blackPen, polygon);

                RectSelect.X = polygon[0].X;
                RectSelect.Y = polygon[0].Y - RectSelect.Height;
                dc.DrawRectangle(blackPen, RectSelect);
                SolidBrush blackBrush = new SolidBrush(Color.Black);
                dc.FillRectangle(blackBrush, RectSelect);

                RectSelect.X = polygon[1].X;
                RectSelect.Y = polygon[1].Y;
                dc.DrawRectangle(blackPen, RectSelect);
                dc.FillRectangle(blackBrush, RectSelect);

                RectSelect.X = polygon[2].X - RectSelect.Width;
                RectSelect.Y = polygon[2].Y;
                dc.DrawRectangle(blackPen, RectSelect);
                dc.FillRectangle(blackBrush, RectSelect);

                RectSelect.X = polygon[3].X - RectSelect.Width;
                RectSelect.Y = polygon[3].Y - RectSelect.Height;
                dc.DrawRectangle(blackPen, RectSelect);
                dc.FillRectangle(blackBrush, RectSelect);


            }

            dc.ResetTransform();

            string line = string.Empty;

            if (noFilesSelected == 1)
            {
                line = FormIniz.nameElement;
                line = string.Concat(line, "  -  ", IDtavol);
            }
            else
            {
                int iElem = 0;
                for (int i = 0; i < 4; ++i)
                {
                    string ss = UnioneDialog.fileUnione[i];
                    if (ss != string.Empty)
                    {
                        if (iElem > 0) line = string.Concat(line, " / ");
                        line = string.Concat(line, ss.Trim());
                        ++iElem;
                        if (iElem == noFilesSelected) break;
                    }
                }
            }

            this.textBox1.Text = line;

            Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
            int Awidth = workingRectangle.Width;

            int Awidth1 = SystemInformation.PrimaryMonitorSize.Width;
            float fscala = 0F;
            if (scalaXY == 1)
            {
                float RGdiff = RGmax.Y - RGmin.Y;
                float RGdiff1 = 1.13F * RGdiff * 25.4F / dc.DpiY;    // = 0.298979 * RGdiff
 
                fscala = Xdiff * 1000.0F / RGdiff1;
                scalaIniziale = (int)fscala;
            }
            else
            {
                fscala = scalaIniziale / scalaXY;
            }

            int iscala = (int)fscala;
            line = "1 : ";
            line = string.Concat(line, iscala.ToString());
            this.textBox2.Text = line;
            drawMapQ = 0;

            this.panel2.Invalidate();
            this.panel3.Invalidate();
 
            attesaFlag = 0;

            if (option_selected == 4)
            {
                selected_point = -1;
                SelezLinea dlg = new SelezLinea();
                DialogResult dr = dlg.ShowDialog();

                Select_flag = 0;
                tipo_selezione = 0;
                option_selected = 0;
                this.Cursor = Cursors.Default;
                if(dr == DialogResult.OK) panel1.Invalidate();

            }
        }
 
        private Point originalPanel1Location;
        private Size originalPanel1Size;
        private Point originalPanel2Location;
        private Size originalPanel2Size;
        private Point originalPanel3Location;
        private Size originalPanel3Size;

        private void ViewQ_SizeChanged(object sender, EventArgs e)
        {
             panel1Size();
           if (scalaXY <= 1F)
           {
               vScrollBar1.Visible = false;
               hScrollBar1.Visible = false;
           }
   
        }

        private void ViewQ_Activated(object sender, EventArgs e)
        {
            MainWindow.formViewQQ = Form.ActiveForm.ActiveMdiChild;
        }

        private void ViewQ_FormClosing(object sender, FormClosingEventArgs e)
        {
            readViewQ = 0;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            Select_flag = 0;
            isel_selected = 0;
            tipo_selezione = 0;
            noFilesSelected = 0;
            UnioneDialog.callOpenUnione = 0;

            GC.Collect();
            GC.WaitForPendingFinalizers();

            this.Close();
        }

         int newY, oldY;
        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            newY = e.NewValue;
        }

        private void vScrollBar1_MouseLeave(object sender, EventArgs e)
        {
            if (newY != oldY)
            {
                oldY = newY;
                panel1.Invalidate();
            }
        }

        int newX, oldX;
        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {

            newX = e.NewValue;
        }


        private void hScrollBar1_MouseLeave(object sender, EventArgs e)
        {
            if (newX != oldX)
            {
                //              MessageBox.Show(newY.ToString());
                oldX = newX;
                panel1.Invalidate();
            }
        }


        static public int tipo_selezione;
        static public int inserimento_modo;

        private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            if (Select_flag != 0)
            {
                Select_flag = 0;
                tipo_selezione = 0;
                if (panel1.Contains(ctx)) panel1.Controls.Remove(ctx);
                panel1.Invalidate();
                return;
            }
            
            PointF Hit = new PointF();
            PointF Hit1 = new PointF();

            double angleRad = -alpha * Math.PI / 180.0D;

            Hit1.X = (float)((double)e.X * Math.Cos(angleRad) - (double)e.Y * Math.Sin(angleRad)) - Xoff;
            Hit1.Y = (float)((double)e.X * Math.Sin(angleRad) + (double)e.Y * Math.Cos(angleRad)) - Yoff;

            Hit.X = (float)(Hit1.X / scalaX) + Xmin;
            Hit.Y = (float)(Hit1.Y / scalaY) + Ymin;

            switch (tipo_selezione)
            {
                case (int)flagSelez.area:
                    selezioneArea(Hit);
                    break;
                case (int)flagSelez.linea:
                    selezioneLinea(Hit);
                    break;
                case (int)flagSelez.punto   :
                    selezionePoint(Hit);
                    break;
                case (int)flagSelez.testo:
                    selezioneTesto(Hit);
                    break;
                default:
                    break;
            }
        }


        private Rectangle RcDraw;

        public static int initial_flag;
        public static int deleted_points;
        private static int max_deleted = 20;
        private static int[] points_deleted = new int[max_deleted];
        private static int max_split = 6;
        private static int[] split_points = new int[max_split];

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            Point Hit = new Point();
            Point Hit1 = new Point();
            double angleRad = -alpha * Math.PI / 180.0D; 

            Hit1.X = (int)((double)e.X * Math.Cos(angleRad) - (double)e.Y * Math.Sin(angleRad)) - Xoff;
            Hit1.Y = (int)((double)e.X * Math.Sin(angleRad) + (double)e.Y * Math.Cos(angleRad)) - Yoff;

            Hit.X = (int)(Hit1.X / scalaX) + Xmin;
            Hit.Y = (int)(Hit1.Y / scalaY) + Ymin;

            RcDraw.X = e.X;
            RcDraw.Y = e.Y;

            if (Select_flag == 0) goto L20;

            if (tipo_selezione == (int)flagSelez.linea)
            {
                if (panel1.Contains(ctx)) panel1.Controls.Remove(ctx);

                if (e.Button == MouseButtons.Right)
                {
                    switch (right_button_option)
                    {
                        case 1:     //  spostamento nodi
                            break;
                        case 2:     //  cacellazione nodi
                            if (deleted_points == 0) break;
                            string caption = "cancellazione punti linea";
                            var result = MessageBox.Show("si vuole cancellare " + deleted_points.ToString() + " punti ?", caption,
                              MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (result == DialogResult.Yes)
                            {
                                elimina_nodi(points_deleted, deleted_points);
                                vectShapes[selected_linea].modifica = 4;
                                Save.modificaLinea = 1;
                            }
                            break;

                        case 3:
                            break;

                        case 5:
                            SelezLinea dlg = new SelezLinea();
                            DialogResult dr = dlg.ShowDialog();
                            break;

                        case 6:       //    conferma  per inserimento nuova linea
                           string caption1 = "inserimento punti";
                           string arg1 = "si vuole confermare l'inserimento dei punti ";
                            var result1 = MessageBox.Show(arg1, caption1,
                                          MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (result1 == DialogResult.Yes) inserimentoLinea2();
                            newPnt = 0;
                            vectShapes[selected_linea].modifica = 4;
                            Save.modificaLinea = 1;
    
                        break;
                            
                        case 7:     
                            break;

                        case 8:     //  conferma nodo per inserimento nuovi nodi
                            string caption2 = "suddivisione linea";
                            string arg2 = "si vuole confermare la suddivisione della linea  " ;
                            int inodi = splitNodo + 1;
                            arg2 += "in  " + inodi.ToString() + " parti ?"; 
                            var result2 = MessageBox.Show(arg2, caption2,
                                          MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (result2 == DialogResult.Yes) suddivisione_linea();                        
                            break;

                        default:
                            break;
                    }

                    insNodiDlg.select_point = 0;
                    flagCursor = 0;
                    isel_selected = 0;
                    Select_flag = 0;
                    option_selected = 0;
                    right_button_option = 0;
                    tipo_selezione = 0;
                    selected_linea = -1;
                    this.Cursor = Cursors.Default;

                    panel1.Invalidate();

                    return;
                }

                if (e.Button == MouseButtons.Left)
                {
                    switch (option_selected)
                    {
                        case 1:     //  spostamento nodi
                            int iret = selectCircle(Hit);
                            if (iret > 0)
                            {
                                DragDropHandler.RegisterControl(panel1, true, true);

                                MyControlLinea ctrl = new MyControlLinea();
                                ctrl.Parent = panel1;
                                ctrl.Visible = true;
                                ctrl.colore = Color.Red;
                                Size sz = new Size(6, 6);
                                Point Pnt = new Point(0, 0);

                                int ipnt = vectShapes[selected_linea].pntpL + selected_point;
                                Pnt.X = (int)((PL[ipnt].X - Xmin) * scalaX) + Xoff;
                                Pnt.Y = (int)((PL[ipnt].Y - Ymin) * scalaY) + Yoff;
                                angleRad = -alpha * Math.PI / 180.0D; // 0F;

                                Hit1.X = (int)((double)Pnt.X * Math.Cos(angleRad) + (double)Pnt.Y * Math.Sin(angleRad));
                                Hit1.Y = (int)(-(double)Pnt.X * Math.Sin(angleRad) + (double)Pnt.Y * Math.Cos(angleRad));

                                ctrl.Location = Hit1;  // Pnt;
                                ctrl.Size = sz;
                                DragDropHandler.RegisterControl(ctrl, false, false);
                                ctx = ctrl;

                                right_button_option = 1;
                            }
                            break;

                        case 2:     //  cancellazione nodi
                            if (deleted_points == 0)
                            {
                                for (int i = 0; i < max_deleted; ++i) points_deleted[i] = 0;
                            }
                            if (deleted_points == max_deleted - 1)
                            {
                                MessageBox.Show("eccessivo numero di punti da cancellare; inviare right mouse down");
                                return;
                            }
                            int iret1 = selectCircle(Hit);
                            if (iret1 > 0)
                            {
                                for (int i = 0; i < deleted_points; ++i)
                                {
                                    if (points_deleted[i] == selected_point) break;
                                }
                                points_deleted[deleted_points] = selected_point;
                                ++deleted_points;
                            }
                            right_button_option = 2;
                            flagMouseMove = 1;
                            break;

                        case 3:     //      selezione nodo per inserimento nuovi punti

                            deleted_points = 0;
                            int iret3 = selectCircle1(Hit);
                            if (iret3 >= 0)
                            {
                                right_button_option = 6;
                                insNodiDlg.insert_point = selected_point + 1;
                                option_selected = 7;
                               inserimento_modo = 3;
                               this.Cursor = Cursors.Cross;
                            }
                            break;

                        case 4:

                            selected_point = -1;
                            selectCircle(Hit);
                            SelezLinea dialog = new SelezLinea();
                            DialogResult dr = dialog.ShowDialog();

                            Select_flag = 0;
                            tipo_selezione = 0;
                            option_selected = 0;
                            this.Cursor = Cursors.Default;
                            panel1.Invalidate();

                            break;

                        case 5:     //  inserimento nuova linea
                        case 6:     //  ampliamento ultima linea
                            inserimentoLinea(Hit);
                            if (inserimento_modo == 1) right_button_option = 5;
                            else right_button_option = 6;
                            break;

                        case 7:     //  inserimento nodi
                            inserimentoLinea(Hit);
                            right_button_option = 6;
                            break;
                        case 8:     //  selezione nodi per suddivisione linea
                            if (splitNodo == max_split) {
                                MessageBox.Show("eccessivo numero di suddivisioni della linea");
                            }
                            else
                            {
                                split_points[splitNodo] = selezioneNodo(Hit);
                                ++splitNodo;
                                right_button_option = 8;
                            }
                            break;
                        default:
                            break;
                    }

                    return;
                }
            }

            if (tipo_selezione == (int)flagSelez.testo)
            {
                if (e.Button == MouseButtons.Right)
                {
                    SelezTesto.selezTestoFlag = 0;
                    SelezTesto dialog = new SelezTesto();
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        panel1.Invalidate();
                    }
                }
                Select_flag = 0;
                tipo_selezione = 0;
                if (panel1.Contains(ctx)) panel1.Controls.Remove(ctx);

                return;
            }

            if (tipo_selezione == (int)flagSelez.punto)
            {

                if (e.Button == MouseButtons.Right)
                {
                    SelezPoint dlg = new SelezPoint();
                    SelezPoint.iniz = 0;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        Save.modificaPunto = 1;
                        panel1.Invalidate();
                    }

                }
     //  tipo_selezione = 13  inserimento punto
     //  tipo_selezione = 3  selezione punto
                isel_selected = 0;
                Select_flag = 0;
                tipo_selezione = 0;
                this.Cursor = Cursors.Default;
                if (panel1.Contains(ctx)) panel1.Controls.Remove(ctx);
                return;
            }

            if (tipo_selezione == (int)flagSelez.area)
            {
                if (panel1.Contains(ctx)) panel1.Controls.Remove(ctx);

                if (e.Button == MouseButtons.Right)
                {
                    SelezArea dialog = new SelezArea();
                    DialogResult dr = dialog.ShowDialog();
                    //               if (dr == DialogResult.OK)
                    {
                        Select_flag = 0;
                        tipo_selezione = 0;
                        this.Cursor = Cursors.Default;
                        panel1.Invalidate();
                    }
                    return;

                }
                //
                //  
                //
                return;
     /*
                int iret = selectCircle(Hit);
                if (iret > 0)
                {

                    MyControlLinea ctrl = new MyControlLinea();
                    ctrl.Parent = panel1;
                    ctrl.Visible = true;
                    ctrl.colore = Color.Red;
                    Size sz = new Size(6, 6);
                    Point Pnt = new Point(0, 0);

                    int ipnt = vectShapes[selected_linea].pntpL + selected_point;
                    Pnt.X = (int)((PL[ipnt].X - Xmin) * scalaX) + Xoff;
                    Pnt.Y = (int)((PL[ipnt].Y - Ymin) * scalaY) + Yoff;

                    angleRad = alpha * Math.PI / 180.0D; // 0F;

                    Hit1.X = (int)((double)Pnt.X * Math.Cos(angleRad) - (double)Pnt.Y * Math.Sin(angleRad));
                    Hit1.Y = (int)((double)Pnt.X * Math.Sin(angleRad) + (double)Pnt.Y * Math.Cos(angleRad));

                    ctrl.Location = Hit1;  // Pnt;
                    ctrl.Size = sz;
                    DragDropHandler.RegisterControl(ctrl, false, false);
                    ctx = ctrl;

                }
                else
                {

                }
                return;
*/
            }
 L20:                   DragDropHandler.RegisterControl(panel1, true, true);

            //		    nessun tipo selezionato (linea/testo/punto/area)

            if (isel_selected > 0)
                switch (tipo_selezione)
                {
                    case (int)flagSelez.area:
                        selezioneArea1(Hit);
                        break;
                    case (int)flagSelez.linea:
                        prova(Hit);
                        selezioneLinea1(Hit);
                        panel1.Invalidate();
                        break;
                    default:
                        break;
                }

            if (inserimento_modo == 0) return;

            switch (tipo_selezione)
            {
                case (int)flagSelez.linea:
                    
                    break;
                case (int)flagSelez.punto:
                    selezionePoint1(Hit);
                    tipo_selezione = 0;
                    break;
                case (int)flagSelez.testo:
                    selezioneTesto1(Hit);
                    tipo_selezione = 0;
                    break;
                default:
                    break;
            }
            this.Cursor = Cursors.Default;

        }

        private void panel1_MouseEnter(object sender, EventArgs e)
        {

            switch (tipo_selezione)
            {
                case (int)flagSelez.linea:
                    if(inserimento_modo == 1) this.Cursor = Cursors.Cross;
                    if(inserimento_modo == 2 && initial_flag== 1)
                    {
                        this.Cursor = Cursors.Cross;
                        inserimentoLinea1();
                        initial_flag = 0;
                    }
         //           break;
                    /**/
                    if (inserimento_modo == 3)
                    { 
                    if(option_selected == 6)
                    {
                        this.Cursor = Cursors.Cross;

              //          inserimentoLinea1();
                    }
            }
                    break;
                   /*  */ 
                case 13:
                    this.Cursor = Cursors.Cross;
                    break;
                case 14:
                    this.Cursor = Cursors.Cross;
                    break;
                default:
                    break;
            }
        }
 

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            //
            //  asse verticale
            //
            float X0, Y0, X1, Y1;
            Pen blackPen = new Pen(Color.Black, 1);
            Font printFont1 = new Font("Arial", 10);
            int Xdiff = Xmax - Xmin;
            int Ydiff = Ymax - Ymin;

            int orizz = this.panel2.Width;
            int ivert = this.panel2.Height;
            int Xright = Xoff + (int)(Xdiff * scalaX);

            Graphics dc = this.panel2.CreateGraphics();

            int i;
            for (i = 0; i < 20; ++i)
            {
                if (retic[i].Y == 0) break;
                Y0 = (retic[i].Y - Ymin) * scalaY + Yoff;
                if (Y0 < 13 || Y0 > ivert - 5) continue;
                X0 = 7;
                X1 = orizz - 8;
                Y1 = Y0;

                float yrot = (vert[2].Y - vert[1].Y) * scalaY;

                string ss = retic[i].Y.ToString();
                int length = ss.Length;
                string line = ss.Substring(2, length - 2);
                line = line.Trim();
                X0 = 9;
                SizeF stringSize = new SizeF();
                stringSize = dc.MeasureString(line, printFont1);
                float YY0 = Y0 + stringSize.Width / 2;
                float angle = -90.0F;
                dc.TranslateTransform(X0, YY0);
                dc.RotateTransform(angle);

                dc.DrawString(line, printFont1, Brushes.Black, 0, 0);
                dc.ResetTransform();
            }
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {
            //  asse orizzontale
            float X0, Y0;

            Pen blackPen = new Pen(Color.Black, 1);
            Font printFont1 = new Font("Arial", 10);
            int orizz = this.panel3.Width;
            int ivert = this.panel3.Height;
            int Xdiff = Xmax - Xmin;
            int Ydiff = Ymax - Ymin;

            int Xright = Xoff + (int)(Xdiff * scalaX);
            int XXoff = 0;
            if (scalaXY == 1F)
            {
                int width0 = panel1.Size.Width;
                XXoff = (panel3.Size.Width - width0) / 2;
            }
            Graphics dc = this.panel3.CreateGraphics();

            for (int i = 0; i < 20; ++i)
            {
                if (retic[i].X == 0) break;
                X0 = (retic[i].X - Xmin) * scalaX + Xoff;
                if (X0 <= Xoff || X0 > Xright) continue;
                string ss = retic[i].X.ToString();
                int length = ss.Length;
                string line = ss.Substring(2, length - 2);
                float xrot = (vert[1].X - vert[0].X) * scalaX;
                SizeF stringSize = new SizeF();
                stringSize = dc.MeasureString(line, printFont1);
                float XX0 = X0 - stringSize.Width / 2.0F + xrot / 2.0F + XXoff;
                Y0 = ivert - 20; // +stringSize.Height;
                dc.DrawString(line, printFont1, Brushes.Black, XX0, Y0);
            }
        }

        public void panel1Size()
        {

            int width = this.ClientSize.Width - button2.Width  ;
            int height = originalPanel1Size.Height + this.ClientSize.Height - oldSize.Height;
            Size sz0 = new Size(width, height);
            panel1.Size = sz0;
            Point pnt0 = new Point(0, panel1.Location.Y);
            pnt0.X = (this.ClientSize.Width - width) / 2;
            panel1.Location = pnt0;

            Point pnt1 = new Point(this.ClientSize.Width - oldPosX2, button2.Location.Y);
            button2.Location = pnt1;

            Point pnt3 = new Point(panel1.Location.X + panel1.Size.Width, panel1.Location.Y);
            vScrollBar1.Location = pnt3;

            Size sz = new Size(vScrollBar1.Size.Width, panel1.Size.Height);
            vScrollBar1.Size = sz;

            Point pnt4 = new Point(panel1.Location.X, panel1.Location.Y + panel1.Size.Height);
            hScrollBar1.Location = pnt4;

            Size sz1 = new Size(panel1.Size.Width, hScrollBar1.Size.Height);
            hScrollBar1.Size = sz1;

            int height2 = originalPanel1Size.Height + this.Size.Height - oldSize.Height;
            Size sz2 = new Size(originalPanel2Size.Width, height2);
            panel2.Size = sz2;

            Point pnt5 = new Point(panel1.Location.X, originalPanel1Location.Y - originalPanel3Size.Height);
            panel2.Location = pnt5;

            int width3 = width;
            Size sz3 = new Size(width3, originalPanel3Size.Height);
            panel3.Size = sz3;

            Point pnt6 = new Point(panel1.Location.X, originalPanel1Location.Y - originalPanel3Size.Height);
            panel3.Location = pnt6;
        }
        

         private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (option_selected == 2)
            {
                flagMouseMove = 0;
                return;
            }


            if (e.X < RcDraw.X)
            {
                RcDraw.Width = RcDraw.X - e.X;
                RcDraw.X = e.X;
            }
            else
            {
                RcDraw.Width = e.X - RcDraw.X;
            }

            if (e.Y < RcDraw.Y)
            {
                RcDraw.Height = RcDraw.Y - e.Y;
                RcDraw.Y = e.Y;
            }
            else
            {
                RcDraw.Height = e.Y - RcDraw.Y;
            }
        }

        int flagMouseMove;
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (option_selected == 2 && flagMouseMove== 1)
            {

                RcDraw.Width = RcDraw.X - e.X;
                RcDraw.Height = RcDraw.Y - e.Y;

                Pen redPen = new Pen(Color.Red, 3);

                dc0.DrawRectangle(redPen, RcDraw);

            }
        }

        void elimina_nodi(int[] Xpos, int inum)
        {
            int pntpL = vectShapes[selected_linea].pntpL;
            int nVertices = vectShapes[selected_linea].nVertices;

            int[] ipos = new int[inum];
            int[] XXpos = new int[inum];
            int xMin = 0;
            //
            //      riordino vettore Xpos secondo valori crescenti
            //
            for (int i = 0; i < inum; ++i) ipos[i] = 0;
            for (int j = 0; j < inum; ++j)
            {
                int k = 0;
                for (k = 0; k < inum; ++k)
                {
                    if (ipos[k] == 0)
                    {
                        xMin = Xpos[k];
                        break;
                    }
                }

                int iptx = 0;
                for (int i = 0; i < inum; ++i)
                {
                    if (i != k)
                    {
                        if (xMin > Xpos[i] && ipos[i] == 0)
                        {
                            xMin = Xpos[i];
                            iptx = i;
                        }
                    }
                    if (i == k) iptx = i;
                }
                XXpos[j] = Xpos[iptx];
                ipos[iptx] = 1;
            }

            for (int i = 0; i < inum; ++i)
            {
                int ipnt = pntpL + XXpos[i];
                PL[ipnt].X = 0;
                PL[ipnt].Y = 0;
            }

            int iVertices = nVertices;
            for (int i = 0; i < nVertices; ++i)
            {
            L10:
                if (i == iVertices) break;
                int ipnt = pntpL + i;
                if (PL[ipnt].X == 0)
                {
                    for (int j = i; j < iVertices - 1; ++j)
                    {
                        ipnt = pntpL + j;
                        PL[ipnt] = PL[ipnt + 1];
                    }
                    --iVertices;
                    goto L10;
                }
            }
            vectShapes[selected_linea].nVertices = nVertices - deleted_points;
        }



        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue < 16 || e.KeyValue > 18)
            {
                int icode = 0;

                if (e.Modifiers == Keys.Alt) icode = 1;
                else if (e.Modifiers == Keys.Shift) icode = 2;
                else if (e.Modifiers == Keys.Control) icode = 3;

                if(e.KeyCode == Keys.Left)
            MessageBox.Show("KeyEventArgs   " + e.KeyCode.ToString() + "  " + e.KeyValue.ToString() + "  " +icode.ToString());

            }

        }


    }
}
